﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class FrmDonthuoc : PRE.catalog.FrmBase
    {
        public static string _madonthuoc;

        DAL.DonthuocDAL _dal = new DAL.DonthuocDAL();
        BLL.DonthuocBLL _bll = new BLL.DonthuocBLL();
        public FrmDonthuoc()
        {
            InitializeComponent();
        }





        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus_PhieuTT(false);

            string _ma = _bll.CreateID();
            txtMADONTHUOC.Text = _ma;

            cboSTTPHIEU.ResetText();
            
            cboNGAYKHAM.EditValue = _bll.GetDate();
           
            //txtHoten.Focus();



            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtMADONTHUOC.Text;

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus_PhieuTT(false);
            //txtHoten.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            string id = grvMain.GetFocusedRowCellValue("MADONTHUOC") + "";
            DataTable dtb_report = _dal.select_report(id);

            var frm = new Report.Report_Base();
            var xr = new Report.XRDonthuoc();

            xr.DataSource = dtb_report;
            frm.SetReport(xr);

            frm.Show();
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            //var name = Proper(txtHoten.Text);
            //var address = Proper(txtDiachi.Text);

            if (IsAdd)
            {
                var o = new DAL.Entities.donthuoc()
                {
                    madonthuoc = txtMADONTHUOC.Text,
                    
                    ngaykham= Convert.ToDateTime(cboNGAYKHAM.EditValue),
                  

                    sttphieu = (cboSTTPHIEU.EditValue + "").Trim()
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_PhieuTT(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_PhieuTT(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                //var _mabn = grvMain.GetFocusedRowCellValue("MaBN") + "";
                string _madonthuoc = txtMADONTHUOC.Text;
                var o = new DAL.Entities.donthuoc()
                {
                    madonthuoc = txtMADONTHUOC.Text.Trim(),

                    ngaykham = Convert.ToDateTime(cboNGAYKHAM.EditValue),


                    sttphieu = (cboSTTPHIEU.EditValue + "").Trim()

                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_PhieuTT();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_PhieuTT(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            txtMADONTHUOC.Text = null;
            cboSTTPHIEU.Text = null;
            
            cboNGAYKHAM.EditValue = DateTime.Now;
           
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtMADONTHUOC.DataBindings.Clear();
            cboSTTPHIEU.DataBindings.Clear();
            cboNGAYKHAM.DataBindings.Clear();
            




            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtMADONTHUOC.DataBindings.Add("EditValue", _dtb, ".MADONTHUOC");
            cboSTTPHIEU.DataBindings.Add("EditValue", _dtb, ".STTPHIEU");
            cboNGAYKHAM.DataBindings.Add("EditValue", _dtb, ".NGAYKHAM");
            

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            txtMADONTHUOC.Properties.ReadOnly = isReadOnly;
            cboNGAYKHAM.Properties.ReadOnly = isReadOnly;
           
            cboSTTPHIEU.Properties.ReadOnly = isReadOnly;

           
            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //DataTable tmp = _dal3.Select_Current(DateTime.Now);
            //string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            _dtb = _dal.Select();


            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
            }

            base.LoadData();
        }
        #endregion


        private void FrmDonthuoc_Load(object sender, EventArgs e)
        {
            DataTable _dtb1 = _dal.Select_sttphieu();
            cboSTTPHIEU.Properties.DataSource = _dtb1;

            DataTable _dtb2 = _dal.Select_sttphieu();
            cboNGAYKHAM.Properties.DataSource = _dtb2;
            ChangeStatus_PhieuTT();
            PerformRefresh();
        }

        private void grcMain_Click(object sender, EventArgs e)
        {

        }

        private void btnThemthuoc_Click(object sender, EventArgs e)
        {
            _madonthuoc = grvMain.GetFocusedRowCellValue("MADONTHUOC") + "";

            var frm = new catalog.FrmCothuoc();
            frm._cothuoc = _madonthuoc;
            frm.Show();
        }
     
    }
}
